package com.zqwh.storemonitor.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.zqwh.storemonitor.R;
import com.zqwh.storemonitor.domain.GeTuiMsg;
import com.zqwh.storemonitor.ezviz.AudioPlayUtil;
import com.zqwh.yj.sdk.utils.BxwVoiceUtil;
import com.zqwh.yj.sdk.utils.CountDownTimerUtil;
import com.zqwh.yj.sdk.utils.LogUtils;
import com.zqwh.yj.sdk.utils.RxBus;

import rx.functions.Action1;

import static com.zqwh.storemonitor.StoreMonitorApplication.sContext;
import static com.zqwh.storemonitor.ezviz.AudioPlayUtil.CUSTOMER_COME_IN_SOUND;
import static com.zqwh.storemonitor.ezviz.AudioPlayUtil.ORDER_PAYED_SOUND;

public class NotificationService extends Service {
    private View viewOrderFinish;
    private View viewComeIn;
    private TextView tvOrderDetail;
    private TextView tvComeIn;



    @Override
    public void onCreate() {
        super.onCreate();
        initPushMessageListener();
        createFloatingViewComeIn();
        hideFlotingViewComeIn();
        createFloatingViewOrderFinish();
        hideFloatingViewOrderFinish();
    }

    private void createFloatingViewOrderFinish() {
        viewOrderFinish = LayoutInflater.from(this).inflate(
                R.layout.float_view_order_finish, null);

        WindowManager windowManager = (WindowManager) this.getApplicationContext()
                .getSystemService(Context.WINDOW_SERVICE);
        WindowManager.LayoutParams layout = new WindowManager.LayoutParams();
        layout.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

        layout.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layout.format = PixelFormat.RGBA_8888;
        layout.gravity = Gravity.TOP | Gravity.END;

        layout.width = WindowManager.LayoutParams.WRAP_CONTENT;
        layout.height = WindowManager.LayoutParams.WRAP_CONTENT;

        layout.y = 200;

        tvOrderDetail = viewOrderFinish.findViewById(R.id.tv_order_detail);
        windowManager.addView(viewOrderFinish, layout);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void createFloatingViewComeIn() {
        viewComeIn = LayoutInflater.from(this).inflate(
                R.layout.float_view_come_in, null);
        tvComeIn = viewComeIn.findViewById(R.id.tv_come_in);
        WindowManager windowManager = (WindowManager) this.getApplicationContext()
                .getSystemService(Context.WINDOW_SERVICE);
        WindowManager.LayoutParams layout = new WindowManager.LayoutParams();
        layout.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

        layout.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layout.format = PixelFormat.RGBA_8888;
        layout.gravity = Gravity.TOP | Gravity.END;

        layout.width = WindowManager.LayoutParams.WRAP_CONTENT;
        layout.height = WindowManager.LayoutParams.WRAP_CONTENT;

        layout.y = 100;
        windowManager.addView(viewComeIn, layout);
    }

    private CountDownTimerUtil countDownTimerUtilComeIn = new CountDownTimerUtil(30 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
            hideFlotingViewComeIn();
        }
    };

    private CountDownTimerUtil countDownTimerUtilOrderPayed  = new CountDownTimerUtil(30 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
            hideFloatingViewOrderFinish();
        }
    };;

    private void showFloatingViewComeIn(String content){
        viewComeIn.setVisibility(View.VISIBLE);
        tvComeIn.setText(content);
    }

    private void hideFlotingViewComeIn(){
        viewComeIn.setVisibility(View.GONE);
        tvComeIn.setText("");
    }

    private void showFloatingViewOrderFinish(String content){
        viewOrderFinish.setVisibility(View.VISIBLE);
        tvOrderDetail.setText(content);
    }

    private void hideFloatingViewOrderFinish(){
        viewOrderFinish.setVisibility(View.GONE);
        tvOrderDetail.setText("");
    }

    private void initPushMessageListener() {
        RxBus.getInstance().addSubscription(this, RxBus.getInstance().doSubscribe(GeTuiMsg.class, new Action1<GeTuiMsg>() {
            @Override
            public void call(GeTuiMsg geTuiMsg) {
                if(geTuiMsg.getType().equals("OPEN_DOOR")){
                    showFloatingViewComeIn(geTuiMsg.getContent());
                    BxwVoiceUtil.playOnce(sContext, R.raw.customer_come_in);
                    countDownTimerUtilComeIn.cancel();
                    countDownTimerUtilComeIn.start();
                }else if (geTuiMsg.getType().equals("ORDER_FINISHED")){
                    showFloatingViewOrderFinish(geTuiMsg.getContent());
                    BxwVoiceUtil.playOnce(sContext, R.raw.order_payed);
                    countDownTimerUtilOrderPayed.cancel();
                    countDownTimerUtilOrderPayed.start();
                }
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                LogUtils.error("Rxbus fail : " + throwable.getMessage());
                initPushMessageListener();
            }
        }));
    }
}
